#include "netgame.h"

netgame::netgame(QWidget *parent) : QMainWindow(parent)
{
    this->setFixedSize(1000,800);
    this->setWindowIcon(QPixmap(":/res/R-C.jpg"));//程序app图片
    this->setWindowTitle("对局进行中");//title

    games=new Gamestart;//创建第二个页面对象
    this->gamestatus=true;
    //退出按钮
    QPushButton* exitbtn=new QPushButton("退出对局",this);
    exitbtn->setFixedSize(100,40);
    exitbtn->setFont(QFont("SimHei",12,QFont::Bold));
    exitbtn->move(850,650);
    connect(exitbtn,QPushButton::clicked,[=](){
            gamestatus=false;
            emit game_back();
            emit game_over();


    });



    this->turn=false;

}

void netgame::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/new/prefix1/res/9802.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    pix.load(":/new/prefix1/res/19101659901796309955316845.jpg");
    pix=pix.scaled(650,650);
    painter.drawPixmap(this->width()*0.5-pix.width()*0.5,this->height()*0.5-pix.height()*0.5,pix);

    for(int i=0;i<15;i++)
    {
        for(int j=0;j<15;j++)
        {
            if(games->chess[i][j]==-1)
            {
                pix.load(":/res/white.png");
                pix=pix.scaled(30,30);
                painter.drawPixmap(this->width()*0.5-650*0.5+10+42.85*i,this->height()*0.5-650*0.5+10+j*42.85,pix);
            }
            else if(games->chess[i][j]== 1)
            {
                pix.load(":/res/black.png");
                pix=pix.scaled(30,30);
                painter.drawPixmap(this->width()*0.5-650*0.5+10+42.85*i,this->height()*0.5-650*0.5+10+j*42.85,pix);
            }
        }
    }
}

void netgame::mousePressEvent(QMouseEvent *event)
{
    if(turn==true)
    {
        int x=event->x();
        int y=event->y();
        if(x>=this->width()*0.5-300&&x<=this->width()*0.5+300&&y>=this->height()*0.5-300&&y<=this->height()*0.5+300)
        {
           double m=(x-this->width()*0.5+300)/42.85;
           double n=(y-this->height()*0.5+300)/42.85;
           int i=(x-this->width()*0.5+300)/42.85;
           int j=(y-this->height()*0.5+300)/42.85;

           if(m-double(i)>=0.7)
               i++;
           else if(m-double(i)<=0.3)
               i=i;
           else
               return;

           if(n-double(j)>=0.7)
               j++;
           else if(n-double(j)<=0.3)
               j=j;
           else
               return;

           this->x=i;
           this->y=j;

           if(games->chess_status)
           {
               if(games->chess[i][j]==0)
               {
                 emit sent_black();//games->chess[i][j]=1;
                 //update();
               }
               else
                   return;
           }
           else
           {
               if(games->chess[i][j]==0)
                {
                   emit sent_white();//games->chess[i][j]=-1;

                   //update();
                }
               else
                   return;
           }

         }
    }

 }
bool netgame::winorn(int i,int j)
{
    int n=0;
    int k,l;
    k=i;
    l=j;
    //检测横向
    while (k>-1&&games->chess[k][l]==games->chess[i][j]) {
        k--;
        n++;
    }
    k=i+1;
    while (k<16&&games->chess[k][l]==games->chess[i][j]) {
        k++;
        n++;
    }
    if(n>=5)
        return true;
    //检测纵向
    k=i;
    n=0;
    while (l>-1&&games->chess[k][l]==games->chess[i][j]) {
        l--;
        n++;
    }
    l=j+1;
    while (l<16&&games->chess[k][l]==games->chess[i][j]) {
        l++;
        n++;
    }
    if(n>=5)
        return true;
    //检测左上左下
    k=i;
    l=j;
    n=0;
    while (k>-1&&l>-1&&games->chess[k][l]==games->chess[i][j]) {
        k--;
        l--;
        n++;
    }
    k=i+1;
    l=j+1;
    while (k<16&&l<16&&games->chess[k][l]==games->chess[i][j]) {
        k++;
        l++;
        n++;
    }
    if(n>=5)
        return true;
    //检测右上右下
    k=i;
    l=j;
    n=0;
    while (k>-1&&l<16&&games->chess[k][l]==games->chess[i][j]) {
        k--;
        l++;
        n++;
    }
    k=i+1;
    l=j-1;
    while (k<16&&l>-1&&games->chess[k][l]==games->chess[i][j]) {
        k++;
        l--;
        n++;
    }
    if(n>=5)
        return true;


    return false;

}
